package com.dingwen.treasure.task.quartz.feign.fallback;

import cn.hutool.core.util.StrUtil;
import com.dingwen.treasure.base.constant.ServerConstant;
import com.dingwen.treasure.base.pojo.vo.Result;
import com.dingwen.treasure.base.pojo.vo.ResultGenerator;
import com.dingwen.treasure.task.quartz.feign.TreasureBusinessFeignClient;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 　TreasureBusinessFeignClientFallback: open feign调用: task --> business 的 hystrix 兜底方法
 * 　@author dingwen
 * 　@date 2022/8/10
 */
@Slf4j
@Component
public class TreasureBusinessFeignClientFallback implements FallbackFactory<TreasureBusinessFeignClient> {

    /**
     * throwable
     */
    private Throwable throwable;


    private final TreasureBusinessFeignClient treasureBusinessFeignClient = new TreasureBusinessFeignClient() {
        @Override
        public Result<String> feignTreasureMessageDeliver() {
            String message = StrUtil.format("{}通过feign调用{},出现错误，方法{},异常信息{}",
                    ServerConstant.TREASURE_TASK_QUARTZ, ServerConstant.TREASURE_BUSINESS,
                    "feignTreasureMessageDeliver", throwable.getMessage());
            log.error(message);

            return ResultGenerator.genFailureResult(message);
        }
    };

    @Override
    public TreasureBusinessFeignClient create(Throwable throwable) {
        this.throwable = throwable;
        return treasureBusinessFeignClient;
    }
}
